const fs = require('fs');
const path = require('path');

// 配置选项
const OUTPUT_FILE = 'PROJECT_STRUCTURE.md';
const IGNORE_DIRS = ['node_modules', '.git', '.vscode', 'dist', 'build'];
const IGNORE_FILES = ['.DS_Store', '.gitignore', OUTPUT_FILE];

// 生成目录树
function generateTree(dir, depth = 0, prefix = '') {
  let result = '';
  const items = fs.readdirSync(dir);
  
  items.forEach((item, index) => {
    const fullPath = path.join(dir, item);
    const isLast = index === items.length - 1;
    const stats = fs.statSync(fullPath);
    
    // 跳过忽略的目录和文件
    if (IGNORE_DIRS.includes(item) || IGNORE_FILES.includes(item)) return;
    
    // 当前项的显示前缀
    const connector = isLast ? '└── ' : '├── ';
    const indent = depth > 0 ? prefix + (isLast ? '    ' : '│   ') : '';
    
    // 添加当前项
    result += `${indent}${connector}${item}\n`;
    
    // 如果是目录，递归处理
    if (stats.isDirectory()) {
      const newPrefix = depth > 0 ? prefix + (isLast ? '    ' : '│   ') : '';
      result += generateTree(fullPath, depth + 1, newPrefix);
    }
  });
  
  return result;
}

// 主函数
function main() {
  const rootDir = process.cwd();
  const tree = generateTree(rootDir);
  
  const markdownContent = `# 项目目录结构图\n\n\`\`\`\n${tree}\`\`\`\n\n> 生成时间: ${new Date().toLocaleString()}`;
  
  fs.writeFileSync(OUTPUT_FILE, markdownContent);
  console.log(`✅ 目录结构已生成到: ${OUTPUT_FILE}`);
}

// 执行脚本
main();